/**
 * 项目名：	Rumba Plan
 * 
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2006，所有权利保留。
 * 
 * 文件名：	BProcessLogMgr.java
 * 模块说明：	
 * 修改历史：
 * 2006-11-15 - lxm - 创建。
 */
package com.hd123.rumba.log.biz;

import java.util.List;

import net.sf.hibernate.Hibernate;
import net.sf.hibernate.HibernateException;

import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.log.IProcessLog;
import com.hd123.rumba.log.IProcessLogMgr;
import com.hd123.rumba.runtime.IManager;
import com.hd123.rumba.runtime.NamingMgr;
import com.hd123.rumba.runtime.biz.BManager;

/**
 * 处理过程日志 | 业务层 | 管理员 | 实现类
 * 
 * @author lxm
 * 
 */
public class BProcessLogMgr extends BManager implements IBProcessLogMgr {

  @Override
  public IManager getPerzMgr() throws BusinessException {
    return getLogMgr();
  }

  public BProcessResult getResult(String processUuid) throws BusinessException, HibernateException {
    this.checkParameterNotNull(processUuid, "processUuid");
    List list = getSession().find(
        "from " + getLogMgr().lookupClass(IProcessLog.class) + " pl"
            + " where pl.processUuid = ? order by pl.itemNo", processUuid, Hibernate.STRING);
    if (list.size() == 0)
      return null;
    BProcessResult result = new BProcessResult();
    for (int i = 0; i < list.size(); i++) {
      IProcessLog plog = (IProcessLog) list.get(i);
      BProcessLog blog = new BProcessLog();
      blog.readFrom(plog);
      result.getLogs().add(blog);

      Integer type = plog.getType();
      if (result.getType() == null || result.getType().intValue() <= type.intValue())
        result.readFrom(plog);
    }
    return result;
  }

  private IProcessLogMgr logMgr = null;

  private IProcessLogMgr getLogMgr() throws BusinessException {
    if (logMgr == null)
      logMgr = (IProcessLogMgr) NamingMgr.getInstance().lookupMgr(IProcessLogMgr.class);
    return logMgr;
  }

}
